<template>
    <div class="container-box" id="container-box">
      <el-row>
        <el-col :xl="12" :lg="14" :md="16" :sm="16" :xs="18">
          <el-form :model="productForm" status-icon :rules="rules" ref="productForm" label-width="100px">
            <el-form-item label="产品名称：">
              <el-input v-model="productForm.product_name"></el-input>
            </el-form-item>
            <!-- <el-form-item label="产品价格：">
              <el-input v-model="productForm.product_price"></el-input>
            </el-form-item>
            <el-form-item label="产品库存：">
              <el-input v-model="productForm.product_inventory"></el-input>
            </el-form-item> -->
            <el-form-item label="状态：" prop="active">
              <el-select v-model="productForm.active" placeholder="请选择产品状态">
                <el-option label="可用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('productForm')">立即更新</el-button>
              <el-button @click="resetForm('productForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  
</template>

<script>
import { timestampToDate } from "../../assets/js/utils";

export default {
  data() {
    return {
      productForm: {
        product_id: "",
        product_name: "",
        product_price: "",
        product_inventory: "",
        active: ""
      },
      productData: {},
      params: {
        "access-token": window.localStorage.getItem("token")
      },
      rules: {
        product_name: [{ required: true, message: "请输入产品名称" }],
        active: [{ required: true, message: "请选择状态", trigger: "change" }]
      }
    };
  },
  created: function() {
    this.getProduct();
  },
  methods: {
    getProduct: function() {
      let product_id = this.$route.params.id;

      this.axios
        .get("products/" + product_id, { params: this.params })
        .then(res => {
          let data = res.data.data.product;

          this.productForm = {
            product_id: data.product_id,
            product_name: data.product_name,
            product_price: data.product_price,
            product_inventory: data.product_inventory,
            active: data.active
          };

          this.productData = JSON.parse(JSON.stringify(this.productForm));

          this.resetForm("productForm");
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let token = window.localStorage.getItem("token");
          this.axios
            .patch(
              "products/" +
                this.productForm.product_id +
                "?access-token=" +
                window.localStorage.getItem("token"),
              this.productForm
            )
            .then(res => {
              if (res.data.code === "1000") {
                this.$message({
                  type: "success",
                  message: "更新成功!"
                });

                this.$refs.productForm.clearValidate();
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message
                });
              }
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.productForm = JSON.parse(JSON.stringify(this.productData));
    }
  }
};
</script>

<style lang="scss" scoped>
.container-box {
  overflow: hidden;
}
</style>
